package com.ningxia.water.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ningxia.water.entity.TsApplicantPlan;
import com.ningxia.water.reuslt.Result;
import com.ningxia.water.reuslt.ResultResponse;
import com.ningxia.water.service.ITsApplicantPlanService;
import com.ningxia.water.utils.CommonUtils;
import com.ningxia.water.vo.out.PlanVolumeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/plan")
@Api(tags = "业务管理-配额管理API")
public class PlanVolumeController {

    @Autowired
    private ITsApplicantPlanService applicantPlanService;

    @GetMapping("/getplanvolumelist")
    @ApiOperation("获取计划水量")
    public Result getPlanVolumeList(@RequestParam(value = "applicantName", required = false) String applicantName) {
        List<PlanVolumeVo> list = applicantPlanService.getPlanVolume(applicantName);
        return ResultResponse.getSuccessResult(list);
    }

    @GetMapping("/getpastplanvolume")
    @ApiOperation("获取往年计划用水量")
    public Result getPastVolume(@RequestParam Integer applicantId, @RequestParam String planYear) {
        PlanVolumeVo planVolume = applicantPlanService.getPlanVolumeByYear(applicantId, planYear);
        return ResultResponse.getSuccessResult(planVolume);
    }

    /**
     * 根据传回的参数判断是否已经存在此 数据记录，如果存在进行修改，不存在则新增
     *
     * @param applicantPlan
     * @return
     */
    @PostMapping("/insertplanvolume")
    @ApiOperation("新增或修改用水计划")
    public Result insertPlanVolume(@RequestBody TsApplicantPlan applicantPlan) {
        UpdateWrapper<TsApplicantPlan> wrapper = new UpdateWrapper<TsApplicantPlan>()
                .eq("applicant_id", applicantPlan.getApplicantId())
                .eq("plan_year", applicantPlan.getPlanYear())
                .eq("plan_type", applicantPlan.getPlanType());
        return CommonUtils.commonResult(applicantPlanService.saveOrUpdate(applicantPlan, wrapper));
    }

}
